<template>
  <div class="login-container">
    <el-form
      ref="loginFormRef"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      autocomplete="on"
      label-position="left">
      
      <div class="title-container">
        <h3 class="title">AI助农管理系统</h3>
        <div class="sub-title">后台管理系统</div>
      </div>
      
      <!-- 用户名 -->
      <el-form-item prop="username">
        <el-input
          ref="usernameRef"
          v-model="loginForm.username"
          placeholder="请输入用户名"
          name="username"
          type="text"
          tabindex="1"
          autocomplete="on"
          size="large"
          clearable>
          <template #prefix>
            <el-icon><User /></el-icon>
          </template>
        </el-input>
      </el-form-item>
      
      <!-- 密码 -->
      <el-form-item prop="password">
        <el-input
          ref="passwordRef"
          v-model="loginForm.password"
          :type="passwordVisible ? 'text' : 'password'"
          placeholder="请输入密码"
          name="password"
          tabindex="2"
          autocomplete="on"
          size="large"
          clearable
          @keyup.enter="handleLogin">
          <template #prefix>
            <el-icon><Lock /></el-icon>
          </template>
          <template #suffix>
            <el-icon
              class="password-icon"
              @click="passwordVisible = !passwordVisible">
              <View v-if="passwordVisible" />
              <Hide v-else />
            </el-icon>
          </template>
        </el-input>
      </el-form-item>
      
      <!-- 记住密码 -->
      <div class="login-options">
        <el-checkbox v-model="loginForm.remember">记住密码</el-checkbox>
        <a class="forget-link" href="javascript:;">忘记密码?</a>
      </div>
      
      <!-- 登录按钮 -->
      <el-button
        :loading="loading"
        :disabled="loading"
        type="primary"
        class="login-button"
        size="large"
        @click="handleLogin">
        登录
      </el-button>
    </el-form>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
import { useUserStore } from '@/store/modules/user'
import type { FormInstance, FormRules } from 'element-plus'
import { User, Lock, View, Hide } from '@element-plus/icons-vue'

const router = useRouter()
const route = useRoute()
const userStore = useUserStore()

// 获取重定向地址
const redirect = route.query.redirect?.toString() || '/dashboard'

// 登录表单实例
const loginFormRef = ref<FormInstance>()
const usernameRef = ref<HTMLInputElement>()
const passwordRef = ref<HTMLInputElement>()

// 登录表单数据
const loginForm = reactive({
  username: '',
  password: '',
  remember: false
})

// 表单验证规则
const loginRules = reactive<FormRules>({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
  ]
})

// 密码可见性
const passwordVisible = ref(false)
// 加载状态
const loading = ref(false)

// 登录方法
const handleLogin = async () => {
  if (!loginFormRef.value || loading.value) return
  try {
    await loginFormRef.value.validate()
    loading.value = true
    await userStore.login(loginForm.username, loginForm.password)
    await userStore.getUserInfo()
    ElMessage.success('登录成功')
    router.push(redirect)
  } catch (error: any) {
    if (error?.response?.data?.message) {
      ElMessage.error(error.response.data.message)
    } else if (error?.message) {
      ElMessage.error(error.message)
    } else {
      ElMessage.error('登录失败，请检查用户名和密码')
    }
  } finally {
    loading.value = false
  }
}
</script>

<style lang="scss" scoped>
.login-container {
  width: 100%;
  height: 100vh;
  overflow: hidden;
  background: linear-gradient(to right, #74ebd5, #9face6);
  display: flex;
  justify-content: center;
  align-items: center;
  
  .login-form {
    width: 400px;
    padding: 35px 35px 15px;
    background: #fff;
    border-radius: 12px;
    box-shadow: 0 2px 24px 0 rgba(0, 0, 0, 0.08);
    
    .title-container {
      text-align: center;
      margin-bottom: 30px;
      
      .title {
        font-size: 28px;
        color: #222;
        margin-bottom: 8px;
        font-weight: bold;
      }
      
      .sub-title {
        font-size: 15px;
        color: #aaa;
      }
    }
    
    .el-form-item {
      margin-bottom: 24px;
    }
    
    .login-options {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 18px;
      
      .forget-link {
        font-size: 14px;
        color: #409EFF;
        text-decoration: none;
        
        &:hover {
          text-decoration: underline;
        }
      }
    }
    
    .login-button {
      width: 100%;
      height: 44px;
      font-size: 18px;
      margin-bottom: 10px;
      border-radius: 6px;
    }
    
    .password-icon {
      cursor: pointer;
    }
  }
}

@media (max-width: 500px) {
  .login-form {
    width: 90%;
    padding: 25px 25px 10px;
  }
}
</style> 